package cn.boysoft.module.security.manager.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.boysoft.core.Constants;
import cn.boysoft.core.ServiceCode;
import cn.boysoft.core.annotation.JEEResultConvert;
import cn.boysoft.core.annotation.JEEService;
import cn.boysoft.core.convert.impl.EntityToIdConvert;
import cn.boysoft.core.exception.JEEException;
import cn.boysoft.core.manager.impl.ManagerImpl;
import cn.boysoft.core.model.PageParam;
import cn.boysoft.module.security.manager.ResourceManager;
import cn.boysoft.module.security.manager.RoleManager;
import cn.boysoft.module.security.model.entity.ResourceEntity;
import cn.boysoft.module.security.model.entity.RoleEntity;

/**
 * @author 付厚俊
 * CreateTime 2013-4-5 上午10:29:52
 */

@Service(value="roleManager")
@Transactional
public class RoleManagerImpl extends ManagerImpl<RoleEntity,PageParam> implements RoleManager<RoleEntity,PageParam> ,ServiceCode{
	private final static Logger LOGGER = LoggerFactory.getLogger(RoleManagerImpl.class);
	public final static String CODE="03";
	@Autowired
	ResourceManager resourceManager;
	@Override
	public String getServiceClassCode() {
		return Constants.JEE_SYSTEM_CODE+"_"+Constants.JEE_SECURITY_CODE+"_"+CODE+"_";
	}
	@Transactional(readOnly=true)
	@JEEService(commandId=Constants.JEE_COMMON_CODE_FIRST,name="查询所有角色",needCheckAuthority=false)
	public List<RoleEntity> queryAllRole(){
		return this.queryAll("from RoleEntity" );
	}
	
	
	@Transactional
	@JEEService(commandId=Constants.COMMAND_SAVE,name="保存角色信息")
	@Override
	@JEEResultConvert(convertClass=EntityToIdConvert.class)
	public RoleEntity save(RoleEntity t) {
		if(t == null){
			throw new JEEException(JEEException.PARAM_IS_NULL);
		}
		
		Set<ResourceEntity> tempResources = t.getResources();
		if(tempResources != null){
			Set<ResourceEntity> resources = new HashSet<ResourceEntity>();
			t.setResources(resources);
			for(ResourceEntity resource: tempResources){
				String resourceId = resource.getId();
				ResourceEntity resourceEntity = (ResourceEntity) this.get(ResourceEntity.class, resourceId);
				if(resourceEntity != null){
					resources.add(resourceEntity);
				}else{
					LOGGER.warn("ResourceEntity id=\""+resourceId+"\"  can't find ");
				}
			}
		}
		return super.save(t);
	}
	
	
	@Transactional
	@JEEService(commandId=Constants.COMMAND_UPDATE,name="修改角色信息",needValue=false)
	@Override
	public RoleEntity update(RoleEntity t) {
		if(t == null){
			throw new JEEException(JEEException.PARAM_IS_NULL);
		}
		String id = t.getId();
		if(StringUtils.isEmpty(id)){
			throw new JEEException(JEEException.OBJECT_ID_IS_NULL);
		}
		RoleEntity oldRoleEntity = this.get(id);
		if(oldRoleEntity == null){
			throw new JEEException(JEEException.CANT_NOT_FIND_RECORD);
		}
		BeanUtils.copyProperties(t, oldRoleEntity);
		Set<ResourceEntity> tempResources = t.getResources();
		if(tempResources != null){
			Set<ResourceEntity> resources = new HashSet<ResourceEntity>();
			oldRoleEntity.setResources(resources);
			for(ResourceEntity resource: tempResources){
				String resourceId = resource.getId();
				ResourceEntity resourceEntity = (ResourceEntity) this.get(ResourceEntity.class, resourceId);
				if(resourceEntity != null){
					resources.add(resourceEntity);
				}else{
					LOGGER.warn("ResourceEntity id=\""+resourceId+"\"  can't find ");
				}
			}
		}
		return super.update(oldRoleEntity);
	}
}
